package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.IsEmailNO;
import com.weiyi.mashangzhu.Utils.IsPhoneNum;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IdeaActivity extends BaseActivity {

    @BindView(R.id.idea_content)
    EditText ideaContent;
    @BindView(R.id.idea_contact)
    EditText ideaContact;
    String contact = "";
    String content = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idea);
        ButterKnife.bind(this);

    }


    @OnClick({R.id.back, R.id.idea_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.idea_submit:
                contact = (ideaContact.getText() + "").trim();
                content=ideaContent.getText()+"";
                if ((ideaContent.getText()+"").equals("")){
                    T.showShort(this,"反馈内容不能为空！");
                }else {
                    if (IsPhoneNum.isMobileNO(contact)==false&& IsEmailNO.checkEmail(contact)==false&&IsEmailNO.checkQQ(contact)==false){
                        T.showShort(this,"请输入有效的联系方式！");
                    }else {
                        OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "")
                                .addParams("feedDesc",content)
                                .addParams("feedContact",contact)
                                .url(API.BaseApi_R+API.FeedBack_Api)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        T.showShort(IdeaActivity.this, "数据加载出错");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        try {
                                            JSONObject jsonObject=new JSONObject(response);
                                            int status=jsonObject.getInt("status");
                                            if (status==0){
                                                T.showShort(IdeaActivity.this,"反馈成功！");
                                                IdeaActivity.this.finish();
                                            }else {
                                                T.showShort(IdeaActivity.this,"返回失败！");
                                            }

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                });
                    }
                }
                break;
        }
    }
}
